<!DOCTYPE html>
<html>
	<head th:include="/common/head :: head">
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit|ie-comp|ie-stand">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<meta http-equiv="Cache-Control" content="no-siteapp" />
	</head>
<body>
	<div class="x-body">
		<form class="layui-form" lay-filter="update_form">
			<div class="layui-form-item">
				<label for="id" class="layui-form-label">绩效编码:
				</label>
				<div class="layui-input-inline">
					<input type="text" id="id" name="id" style="border:0px;outline:none"
						 autocomplete="off" class="layui-input" readOnly="readOnly">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="userId" class="layui-form-label">用户姓名:
				</label>
				<div class="layui-input-inline">
					<select  id="userId" name="userId" lay-search="">
						<option></option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label for="level" class="layui-form-label">绩效等级:
				</label>
				<div class="layui-input-inline">
					<button id="plus"  class="layui-btn  layui-btn-sm ">+</button>
					<input type="text" class="layui-btn-sm" size="2" id="level" name="level" />
					<button id="minus" class="layui-btn  layui-btn-sm ">-</button>
				</div>
			</div>
			<div class="layui-form-item">
				<label for="remarks" class="layui-form-label">备注信息:
				</label>
				<div class="layui-input-block">
                  <textarea  id="remarks" name="remarks" autocomplete="off" class="layui-textarea"></textarea>
                </div>
			</div>
			<div class="layui-form-item">
				<label for="L_repass" class="layui-form-label"> </label>
				<button class="layui-btn" lay-filter="update_form" lay-submit="">保存</button>
				<button class="layui-btn" onclick="black()">返回</button>
			</div>
		</form>
	</div>
</body>
</html>
<script>
	$(function(){
		//初始化
		init();
		//绑定事件
		bindEvent();
	})
	function bindEvent(){
		//绩效增加
		$("#plus").bind("click",function(){
			var level= $("#level").val();
			var num =parseInt(level)+1;
			$("#level").val("");
			if(num>=0){
				$("#level").val("+"+num);
			}else{
				$("#level").val(num);
			}
			return false;
		})
		//绩效减少
		$("#minus").bind("click",function(){
			var level= $("#level").val();
			var num =parseInt(level)-1;
			$("#level").val("");
			if(num>=0){
				$("#level").val("+"+num);
			}else{
				$("#level").val(num);
			}
			return false;
		})
	}
	var mentId = sessionStorage.getItem("mentId");
	var _this = {};
	var updateFormData={};
	//通过id拿到对象
	function init(){
		$.post("/achievement/getMentById","mentId="+mentId,function(result){
			if(result.status==0){
				//初始化修改表单（赋值）
				_this.ment=result.data;
				//获取用户姓名、id下拉表
				getUserId();
				//初始化赋值）
				initUpdateForm(result.data);
			}else{
				layer.msg(result.msg);
			}
		})
	}
	//初始化（赋值）
	function initUpdateForm(ment){
		layui.use(['form','layer'],function(){
			$=layui.jquery;
			var form = layui.form,
			layer = layui.layer;
			//赋值
			form.val('update_form',{
				"id":ment.id,
				"remarks":ment.remarks
			})
			if(ment.level>=0){
				form.val('update_form',{
					"level":"+"+ment.level
				})
			}else{
				form.val('update_form',{
					"level":ment.level
				})
			}
			//监听提交
			form.on("submit(update_form)",function(data){
				console.log(data.field);
				updateFormData = data.field;
				updateMent();
				return false;
			})
		})
	}
	//提交修改操作
	function updateMent(){
		$.ajax({
			url:"/achievement/updateMent",
			type:"post",
			data:{
				"id":mentId,
				"userId":updateFormData.userId,
				"level":updateFormData.level,
				"remarks":updateFormData.remarks
			},
			success:function(result){
				if(result.status==0){
					layer.msg(result.msg,function(){
						//关闭弹出层
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);
					})
				}else{
					layer.msg(result.msg);
				}
			}
		})
	}
	//异步改为同步  async:false;
	//获取用户姓名、id列表
	function getUserId(){
		$.ajax({
			url:"/user/getUserIds",
			type:"post",
			async:false,
			success:function(result){
				if(result.status==0){
					showUserName(result);	
				}else{
					layer.msg(result.msg);
				}
			}
		})
	}
	//展示下拉框
	function showUserName(result){
		var str= "";
		var list = result.data;
		for(i in list){
			if(_this.ment.userId==list[i].userName){
				str+='<option selected="selected" value="'+list[i].id+'">'+_this.ment.userId+'</option>';
			}else{
				str+="<option  value="+list[i].id+">"+list[i].userName+"</option>";
			}
		}
		$("#userId").append(str);
	}
	//返回
	function black(){
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	}
	
</script>
